package com.bt.lms.network.msg.packet;

import com.bt.lms.network.msg.ReqMsg;
import com.bt.lms.utils.Convert;

/**
 * 请求消息包
 *
 * @author 李舒淇
 */
public class ReqMsgPacket {

    /**
     * 魔数  12
     */
    private byte[] magicNo;

    /**
     * 协议版本号  1
     */
    private byte version = ReqMsg.NETWORK_VERSION_ONE;

    /**
     * 序列化算法  1
     */
    private byte serialCou = ReqMsg.NETWORK_SERIAL_COU_JSON;

    /**
     * 事件类型  1
     */
    private byte evenType = ReqMsg.NETWORK_EVEN_TYPE_BUSINESS;

    /**
     * 业务类型   2
     */
    private byte[] bizType;

    /**
     * 2Way  1
     */
    private byte twoWay = ReqMsg.NETWORK_TWO_WAY_CHAT;

    /**
     * 请求id 8
     */
    private byte[] reqID;

    /**
     * 保留字段   16
     */
    private byte[] remainField;

    /**
     * 数据长度   4
     */
    private byte[] dataLen;

    /**
     * 数据(消息体)
     */
    private byte[] data;


    /**
     * 转请求消息
     *
     * @return 请求消息
     */
    public ReqMsg toReqMsg() {

        //要返回的对象
        ReqMsg reqMsg = new ReqMsg();

        //1.消息头
        //魔数
        byte[] magicNoBytes = this.getMagicNo();
        String magicNo = new String(magicNoBytes);
        reqMsg.setMagicNo(magicNo);
        //协议版本号
        byte version = this.getVersion();
        reqMsg.setVersion(version);
        //序列化算法
        byte serialCou = this.getSerialCou();
        reqMsg.setSerialCou(serialCou);
        //事件类型
        byte evenType = this.getEvenType();
        reqMsg.setEvenType(evenType);
        //业务类型
        byte[] bizTypeBytes = this.getBizType();
        short bizType = Convert.bytesToShort(bizTypeBytes);
        reqMsg.setBizType(bizType);
        //2way
        byte twoWay = this.getTwoWay();
        reqMsg.setTwoWay(twoWay);
        //请求id
        byte[] reqIDBytes = this.getReqID();
        Long reqId = Convert.bytesToLong(reqIDBytes);
        reqMsg.setReqId(reqId);
        //保留字段
        byte[] remainFieldBytes = this.getRemainField();
        reqMsg.setRemainField(remainFieldBytes);

        //消息体
        byte[] dataBytes = this.getData();
        String data = new String(dataBytes);
        reqMsg.setData(data);

        return reqMsg;
    }

    /**
     * 转请求消息
     *
     * @param sumBytes 总字节
     * @return 请求消息
     */
    public static ReqMsg toReqMsg(byte[] sumBytes) {

        //要返回的对象
        ReqMsg reqMsg = new ReqMsg();

        //数组拷贝角标
        int index = 0;

        //1.消息头
        //魔数
        byte[] magicNoBytes = new byte[ReqMsg.NETWORK_MAGIC_NO_LEN];
        System.arraycopy(sumBytes,index,magicNoBytes,0,ReqMsg.NETWORK_MAGIC_NO_LEN);
        index += ReqMsg.NETWORK_MAGIC_NO_LEN;
        String magicNo = new String(magicNoBytes);
        reqMsg.setMagicNo(magicNo);
        //协议版本号
        byte version = sumBytes[index++];
        reqMsg.setVersion(version);
        //序列化算法
        byte serialCou = sumBytes[index++];
        reqMsg.setSerialCou(serialCou);
        //事件类型
        byte evenType = sumBytes[index++];
        reqMsg.setEvenType(evenType);
        //业务类型
        byte[] bizTypeBytes = new byte[ReqMsg.NETWORK_BIZ_TYPE_LEN];
        System.arraycopy(sumBytes,index,bizTypeBytes,0,ReqMsg.NETWORK_BIZ_TYPE_LEN);
        index += ReqMsg.NETWORK_BIZ_TYPE_LEN;
        short bizType = Convert.bytesToShort(bizTypeBytes);
        reqMsg.setBizType(bizType);
        //2way
        byte twoWay = sumBytes[index++];
        reqMsg.setTwoWay(twoWay);
        //请求id
        byte[] reqIDBytes = new byte[ReqMsg.NETWORK_REQ_ID_LEN];
        System.arraycopy(sumBytes,index,reqIDBytes,0,ReqMsg.NETWORK_REQ_ID_LEN);
        index += ReqMsg.NETWORK_REQ_ID_LEN;
        Long reqId = Convert.bytesToLong(reqIDBytes);
        reqMsg.setReqId(reqId);
        //保留字段
        byte[] remainFieldBytes = new byte[ReqMsg.NETWORK_REMAIN_FIELD_SIZE];
        System.arraycopy(sumBytes,index,remainFieldBytes,0,ReqMsg.NETWORK_REMAIN_FIELD_SIZE);
        index += ReqMsg.NETWORK_REMAIN_FIELD_SIZE;
        reqMsg.setRemainField(remainFieldBytes);
        //数据长度
        byte[] dataLenBytes = new byte[ReqMsg.NETWORK_DATA_LEN];
        System.arraycopy(sumBytes,index,dataLenBytes,0,ReqMsg.NETWORK_DATA_LEN);
        index += ReqMsg.NETWORK_DATA_LEN;
        int dataLen = Convert.bytesToInt(dataLenBytes);

        //消息体
        byte[] dataBytes = new byte[dataLen];
        System.arraycopy(sumBytes,index,dataBytes,0,dataLen);
        String data = new String(dataBytes);
        reqMsg.setData(data);

        return reqMsg;
    }


    public byte[] getMagicNo() {
        return magicNo;
    }

    public void setMagicNo(byte[] magicNo) {
        this.magicNo = magicNo;
    }

    public byte getVersion() {
        return version;
    }

    public void setVersion(byte version) {
        this.version = version;
    }

    public byte getSerialCou() {
        return serialCou;
    }

    public void setSerialCou(byte serialCou) {
        this.serialCou = serialCou;
    }

    public byte getEvenType() {
        return evenType;
    }

    public void setEvenType(byte evenType) {
        this.evenType = evenType;
    }

    public byte[] getBizType() {
        return bizType;
    }

    public void setBizType(byte[] bizType) {
        this.bizType = bizType;
    }

    public byte getTwoWay() {
        return twoWay;
    }

    public void setTwoWay(byte twoWay) {
        this.twoWay = twoWay;
    }

    public byte[] getReqID() {
        return reqID;
    }

    public void setReqID(byte[] reqID) {
        this.reqID = reqID;
    }

    public byte[] getRemainField() {
        return remainField;
    }

    public void setRemainField(byte[] remainField) {
        this.remainField = remainField;
    }

    public byte[] getDataLen() {
        return dataLen;
    }

    public void setDataLen(byte[] dataLen) {
        this.dataLen = dataLen;
    }

    public byte[] getData() {
        return data;
    }

    public void setData(byte[] data) {
        this.data = data;
    }
}
